﻿using SimpleEvent;
using System;
using System.Threading;
using System.Windows.Forms;
using SimpleEvent;

namespace _4委托与事件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //挂接事件
            foreach (Control ctl in groupBox2.Controls)
            {
                if (ctl is TextBox)
                {
                    (ctl as TextBox).KeyDown += this.EnterToTab;
                }
            }
        }
        /// <summary>
        /// 回车移动焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EnterToTab(object sender,KeyEventArgs e)
        {
            TextBox txt = null;
            if (e.KeyCode==Keys.Enter)
            {
                groupBox2.SelectNextControl(sender as Control, true, true, true, true);
                //拥有焦点的文本框自动全选
                txt = (sender as TextBox);
                if (txt!=null)
                {
                    txt.SelectAll();
                }
            }
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            Cat cat = new Cat("Tom");
            Mouse mouse1 = new Mouse("Jerry");
            Mouse mouse2 = new Mouse("Jack");

            cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);
            cat.CatShout += mouse1.Run;//第二种写法
            cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);
            cat.CatShout += mouse2.Run;//第二种写法
            cat.Shout();

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            EventTest a = new EventTest(5);
            a.SetValue(7);
            a.SetValue(11);
            a.SetValue(11);
        }

        private void Label1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = string.Format("事件信息：({0},{1})\n事件源自：{2}", e.X, e.Y, sender.GetType());
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = string.Format("事件信息：({0},{1})\n事件源自：{2}", e.X, e.Y, sender.GetType());
        }


        #region 借钱次数和总数的事件与委托
        private int LoanCount = 0;
        private double LoanMoney = 0;
        private void Button_money_Click(object sender, EventArgs e)
        {
            LoanFromHuang();
        }
        private void LoanFromHuang()
        {
            button_totalMoney.Enabled = true;
            LoanMoney = 0;
            button_totalMoney.Click += Button_totalMoney_Click;
            label_money.Text = string.Format("借款次数：{0}次", ++LoanCount);
        }

        private void ShowLoanState()
        {
            LoanMoney += 100;
            label_totalmoney.Text = string.Format("欠款总额：{0}元", LoanMoney);
            label_totalmoney.Refresh();
            Thread.Sleep(300);
            button_totalMoney.Enabled = false;
        }
        private void Button_totalMoney_Click(object sender, EventArgs e)
        {
            ShowLoanState();
        }
        #endregion
    }
}
